﻿using System.Data;
using JKF.BusinessLayer.BLL;
using JKF.Entities;
using JKF.Entities.Model;
using JKF.Substructure;
using SFWS.XHandler.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace JKF.HandlerLayer.Points
{
    public class Point_ExchangeHandler : AHttpHandler
    {
        private static bllPOINT_EXCHANGE bllExchange = new bllPOINT_EXCHANGE();

        public override void ProcessRequest()
        {
            try
            {
                this.HandleRequest(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                AppLogger.Log(ex.Message, ex);  //写日志
            }
        }

        /// <summary>
        /// 取得兑换主记录列表
        /// </summary>
        public void GetPointExchange()
        {
            bllPOINT_EXCHANGE pointExchange = new bllPOINT_EXCHANGE();
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in pointExchange.GetPointExchange(P("searchItem"), P("matchWay"), P("keyword"), P("dateStart"), P("dateEnd"),P("ownShop"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             EXCHANGE_KEY = a.Field<Guid>("EXCHANGE_KEY").ToString(),
                             EXCHANGE_NO = a.Field<string>("EXCHANGE_NO"),
                             GIFTS = a.Field<string>("GIFTS"),
                             DEDUCT_POINT = a.Field<Int32>("DEDUCT_POINT").ToString(),
                             DATE_EXCHANGE = a.Field<DateTime>("DATE_EXCHANGE").ToString(),
                             MEMO = a.Field<string>("MEMO"),
                             DATE_CREATE = a.Field<string>("DATE_CREATE"),
                             CARD_NO = a.Field<string>("CARD_NO"),
                             NAME = a.Field<string>("NAME"),
                             MOBILE = a.Field<string>("MOBILE"),
                             ACCOUNT = a.Field<string>("ACCOUNT"),
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        public void GetExchangeByKey()
        {
            try
            {
                var scid = P("exchangeKey");
               
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data =  bllExchange.GetExchangeByKey(scid)}, Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 得到兑换明细
        /// </summary>
        public void GetExchangeItemByKey()
        {
            try
            {
                var scid = P("exchangeKey");
                //Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllExchange.GetExchangeItem(scid) }, Utils.GetDateFormat));
                Response.Write(JsonSerializer.Serialize(bllExchange.GetExchangeItem(scid), Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 兑换礼品时选择顾客
        /// </summary>
        public void GetCustomer()
        {
            try
            {
                var searchKeyword = P("keyword");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllExchange.GetCustomer(searchKeyword) }, Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 根据卡号获得顾客信息
        /// </summary>
        public void GetCustomerByCard()
        {
            try
            {
                var searchKeyword = P("cardNo");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllExchange.GetCustomerByCard(searchKeyword) }, Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 取得待选择礼品
        /// </summary>
        public void GetGift()
        {
            bllGIFTS gifts = new bllGIFTS();
            //Response.Write(JsonSerializer.Serialize(gifts.GetList(string.Format(" SHOP_CODE='{0}' or SHOP_CODE IS NULL",bllSYS_USER.CurrentUser.SHOP_CODE.ToString())), Utils.GetDateFormat));
            Response.Write(JsonSerializer.Serialize(gifts.GetList("  "), Utils.GetDateFormat));
        }

        /// <summary>
        /// 保存兑换记录及明细
        /// </summary>
        public void SaveExchange()
        {
            var EXCHANGE_KEY = P("EXCHANGE_KEY");
            var customerKey = P("customerKey");
            var items = P("items");
            
            try
            {
                bllPOINT_EXCHANGE.ExchangeItemJson[] itemObj = JsonSerializer.Deserialize<bllPOINT_EXCHANGE.ExchangeItemJson[]>(items);
                var pk = bllExchange.SaveExchange(EXCHANGE_KEY, customerKey,itemObj);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message ="操作成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void DeleteExchange()
        {
            try
            {
                var exchangeKey = P("exchangeKey");
                bllExchange.DeleteExchange(exchangeKey);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = exchangeKey }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void GetPersonExchange()
        {
            bllPOINT_EXCHANGE pointExchange = new bllPOINT_EXCHANGE();
            var customerKey = P("customerKey");
            Response.Write(JsonSerializer.Serialize(pointExchange.GetPersonExchange(customerKey), Utils.GetDateFormat));
        }

        public void SuggestCustomer()
        {
            var keyWord = P("qry");
            //页数[第一页为0]
            var page = Convert.ToInt32(Request["_pageNo"]);

            Response.Write(JsonSerializer.Serialize(bllExchange.SuggestCustomer(keyWord, page)));
        }
    }
}
